package com.yx.cloud.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.bean.MessageCenterBean;
import com.yx.cloud.utils.RomUtils;
import com.yx.cloud.utils.StringUtils;
import com.iot.common.utils.LogUtils;

import java.util.List;

public class MessageInformAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private final static String TAG = "ListAdapter";
    private Context mContext;
    private List<MessageCenterBean> beanList;
    private OnItemClickListener mOnItemClickListener = null;
    private onChecked checked;

    public onChecked getChecked() {
        return checked;
    }

    public void setChecked(onChecked checked) {
        this.checked = checked;
    }

    public MessageInformAdapter(Context context, List list) {
        mContext = context;
        beanList = list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.message_inform_adapter_item, parent, false);
        ViewHolder holder = new ViewHolder(view);
        view.setOnClickListener(this);
        return holder;
    }


    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        MessageCenterBean bean = beanList.get(position);

        ((ViewHolder) holder).tvName.setText(bean.getTitle());
        long time = bean.getTime();
        ((ViewHolder) holder).tvTime.setText(StringUtils.timeStamp2Date(time, null) + "");
        if (bean.isTag()) {
            ((ViewHolder) holder).ivSelect.setVisibility(View.VISIBLE);
        } else {
            ((ViewHolder) holder).ivSelect.setVisibility(View.GONE);
        }
        ((ViewHolder) holder).ivSelect.setChecked(bean.isSelected());
        ((ViewHolder) holder).ivSelect.setOnCheckedChangeListener(null);//清掉监听器

        ((ViewHolder) holder).ivSelect.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (buttonView.isPressed()) {
                    LogUtils.i(position + " : MessageCenterActivity onCheckedChanged : " + isChecked);
                    beanList.get(position).setSelected(isChecked);//把选中的状态保存到list中去
                    checked.CheckedChanged(position, isChecked, buttonView); //给按钮设置回调
                }
            }
        });
        holder.itemView.setTag(position);
    }

    public void updateData(List<MessageCenterBean> list) {
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * Item点击事件方法
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    public interface onChecked {
        void CheckedChanged(int position, boolean isChecked, CompoundButton buttonView);
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        public CheckBox ivSelect;
        public TextView tvName, tvTime;

        public ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_informName);
            tvTime = itemView.findViewById(R.id.tv_informTime);
            ivSelect = itemView.findViewById(R.id.iv_inform_select);
        }
    }

}
